<?php
App::uses('AppController', 'Controller');
/**
 * Brands Controller
 *
 * @property Brand $Brand
 */
class BrandsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Brand->recursive = 0;
		$this->set('brands', $this->paginate());

        $this->pageTitle = '- Brands';
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Brand->exists($id)) {
			throw new NotFoundException(__('Invalid brand'));
		}
		$options = array('conditions' => array('Brand.' . $this->Brand->primaryKey => $id));
		$this->set('brand', $this->Brand->find('first', $options));
        //find('list', array('order' => array('product.name' => 'ASC')));

        $this->pageTitle = '- View a Brand';

    }

/**
 * add method
 *
 * @return void
 */
    public function add() {

        $this->pageTitle = '- Add a Brand';

        if ($this->request->is ( 'post' )) {
            $this->Brand->create ();
            // save data
            $result = $this->Brand->save ( $this->request->data );
            $msg = array (
                __ ( 'The brand has been saved' ),
                __ ( 'The brand could not be saved. Please, try again.' )
            );
            // if ajax request, then back to json data
            if ($this->request->is ( "ajax" )) {
                header ( "Pragma: no-cache" );
                header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
                header ( 'Content-Type: text/x-json' );
                header ( 'Content-type: application/json' );
                $brandlist = array ();
                $status=false;
                if ($result) {
                    $brand_all = $this->Brand->find ( "all" , array('order' => array('brand.name' => 'ASC')));
                    if (! empty ( $brand_all )) {
                        foreach ( $brand_all as $v ) {
                            $brandlist[]=$v["Brand"];
                        }
                    }
                    $status=true;
                }
                $data = array (
                    "status" => $status,
                    "message" => $status ? $msg [0] : $msg [1],
                    "brandlist" => $brandlist
                );
                echo json_encode ( $data );
                die ();
            } elseif ($result) {
                $this->Session->setFlash ( $msg [0] );
                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash ( $msg [1] );
            }
        }
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Brand->exists($id)) {
			throw new NotFoundException(__('Invalid brand'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Brand->save($this->request->data)) {
				$this->Session->setFlash(__('The brand has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The brand could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Brand.' . $this->Brand->primaryKey => $id));
			$this->request->data = $this->Brand->find('first', $options);
		}
        $this->pageTitle = '- Edit a Brand';

    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Brand->id = $id;
		if (!$this->Brand->exists()) {
			throw new NotFoundException(__('Invalid brand'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Brand->delete()) {
			$this->Session->setFlash(__('Brand deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Brand was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
        
        //----------------------------------------------------------------------
        
        public function beforeFilter() {
            parent::beforeFilter();
            
            $this->Auth->autoRedirect = false;
            
            $this->Auth->deny('*');   
        }

    public function search() {
        //$this->set('results',$this->Customer->search($this->data['Customer']['q']));

        $keyword=$this->request->data['Brands']['keyword']; //get keyword from querystring//
        //used simpme or condition with singe value checking
        //replace ModelName with actual name of your Appmodel

        //$this->Session->setFlash($keyword);

        $conditions=array('OR'=>array(
            'Brand.id LIKE'=> $keyword,
            'Brand.name LIKE'=> $keyword));

        $this->set('isFound', $this->Brand->find('count',array(
                    'conditions'=>$conditions)
            )
        );

        $this->set('list', $this->Brand->find('all',array(
                    'conditions'=>$conditions)
            )
        );
    }



}
